package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {
Fragment fragment1,fragment2,fragment3,fragment4;
LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
FragmentManager manager;
FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        linearLayout1=findViewById(R.id.linearLayout_1);
        linearLayout2=findViewById(R.id.linearLayout_2);
        linearLayout3=findViewById(R.id.linearLayout_3);
        linearLayout4=findViewById(R.id.linearLayout_4);


        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragment4=new Fragment4();

        manager=getSupportFragmentManager();
        transaction =manager.beginTransaction();

        transaction.add(R.id.framelayout1,fragment1);
        transaction.add(R.id.framelayout1,fragment2);
        transaction.add(R.id.framelayout1,fragment3);
        transaction.add(R.id.framelayout1,fragment4);

//        intial();
        Fragment_hide();
        //show展示一个页面
        transaction.show(fragment1);
        transaction.commit();
        linearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction =manager.beginTransaction();
                Fragment_hide();
                transaction.show(fragment1);
                transaction.commit();
            }
        });
        linearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction =manager.beginTransaction();
             Fragment_hide();
             transaction.show(fragment2);
             transaction.commit();
            }
        });
        linearLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction =manager.beginTransaction();
                Fragment_hide();
                transaction.show(fragment3);
                transaction.commit();
            }
        });
        linearLayout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction =manager.beginTransaction();
                Fragment_hide();
                transaction.show(fragment4);
                transaction.commit();
            }
        });



    }

//    private void intial() {
//
//
//
//    }


    void Fragment_hide(){
        //这里hide是隐藏我们的控件
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
    }
}

